# -*- coding: utf-8 -*-
# FileName:     key_label.py
# time:         22/11/25 025 下午 11:14
# Author:       Zhou Hang
# Description:  I don't want to write
import pathlib

from PyQt5.QtCore import Qt, QTimer, QPropertyAnimation, QPointF
from PyQt5.QtGui import QFont, QPainterPath, QPainter
from PyQt5.QtWidgets import QLabel, QGraphicsOpacityEffect


class KeyLabel(QLabel):
    def __init__(self, text, parent=None):
        super(KeyLabel, self).__init__(text, parent)
        # self.setWordWrap(True)
        self.path = None
        self.timer = None
        self.setAlignment(Qt.AlignTop)
        qss = pathlib.Path('../qss/key_label.qss')
        self.setStyleSheet(qss.read_text())

        # 获得控件的左上角坐标
        self.x = 0
        self.y = 0

        self.opacity = QGraphicsOpacityEffect()  # 透明度对象
        self.opacity.setOpacity(1)  # 初始化设置透明度为1
        self.setGraphicsEffect(self.opacity)  # 把标签的透明度设置为为self.opacity

        self.draw()  # 淡出效果开始

    def draw(self):
        """label淡出效果函数"""
        self.opacity.i = 100  # 用于记录透明度变化与循环次数

        def timeout():  # 超时函数：改变透明度
            self.opacity.setOpacity(self.opacity.i / 100)
            self.setGraphicsEffect(self.opacity)  # 改变标签透明度
            self.opacity.i -= 1
            if self.opacity.i < 0:  # 此时透明度为0，控件完全消失
                self.timer.stop()  # 计时器停止
                self.timer.deleteLater()
                # self.deleteLater()

        self.timer = QTimer()  # 计时器
        self.timer.setInterval(30)  # 设置间隔时间，毫秒为单位
        self.timer.timeout.connect(timeout)  # 超时槽函数，每到达间隔时间，调用该函数
        self.timer.start()  # 计时器开始

    def upward_move(self):
        """向上移动控件"""
        self.x = self.pos().x()
        self.y = self.pos().y()
        # start_point = QPointF(self.x, self.y)
        # end_point = QPointF(self.x, self.y - self.height() - 20)

        self.move(self.x, self.y - self.height() - 20)

        # self.path = QPainterPath()
        # self.path.moveTo(self.x, self.y)
        # self.path.lineTo(self.x, self.y - self.height() - 10)
        #
        # anim = QPropertyAnimation(self, b'pos')
        # anim.setDuration(3000)
        # anim.setStartValue(start_point)
        #
        # vals = [p / 100 for p in range(0, 101)]
        # for i in vals:
        #     anim.setKeyValueAt(i, self.path.pointAtPercent(i))  # 设置100个关键帧
        #
        # anim.setEndValue(end_point)
        # anim.start()


def main():
    pass


if __name__ == "__main__":
    main()
